<template>
  <uni-nav-bar title="身份选择" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package space-between" v-for="item in identityList" :key="item.id" @click="tanChange(item.page,item.id)">
    <view>{{item.name}}</view>
    <uni-icons type="forward" size="25" color="#999"></uni-icons>
  </view>
</template>

<script setup lang="ts">
  import { ref } from 'vue';
  import { navBack } from '@/utils/navigator';
  import { toPublish } from '@mqtt';
  import { getShenList } from '@/gql/administrator';
  import { errorToast, showLoading } from '@/utils/prompt';
  import { onLoad } from '@dcloudio/uni-app';
  import { getDriverID } from '@/stores/driverID';
  const driver_id = getDriverID();
  const type = ref('')
  const identityList = ref([])
  onLoad((pearm) => {
    type.value = pearm.type
    init()
  })
  function init() {
    showLoading()
    const payload = {
      query: getShenList,
      variables: {
        type: type.value
      },
    };
    toPublish(
      'ql/control/getShenList',
      payload,
      (obj : any) => {
        const { getShenList } = obj.data;
        identityList.value = getShenList
      }
    );
  }
  function tanChange(page : string, id : number) {
    showLoading()
    const payload = {
      type: type.value,
      user_id: driver_id,
      item_id: id
    };
    toPublish(
      'control/editId',
      payload,
      (obj : any) => {
        const { code, msg } = obj;
        if (code === 1) {
          location.href = page
        } else {
          errorToast(msg)
        }
      }
    );
  }
</script>

<style scoped lang="less">
</style>